package com.ifgostudio.framework.web.filter;

import java.io.IOException;
import java.util.Map;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.ifgostudio.framework.context.WebContext;
import com.ifgostudio.framework.web.WebContextHelper;

/**
 * <b>Description</b>:WEB请求上下文环境过滤器<br/>
 * <b>Author</b>: linxuexin<br/>
 * <b>Link</b>: Email:<a href="mailto:linxuexin0423@gmail.com">linxuexin0423@gmail.com</a>;Telephone:13799286849;QQ:30268646;林先生<br/>
 * <b>Copyright</b>: <a href="http://www.ifgostudio.com" target="_blank">www.ifgostudio.com</a> all rights reserved<br/>
 * <b>Information</b>: 对外承接网站制作,价格优惠,服务一流,强有力的技术支持,asp,Jsp,PHP,MySQL,Hibernate,Struts2,MS SQLServer,Oracle,jQuery,Css+div布局,Dhtml<br/>
 * <b>Create Date</b>: 2013-3-8 下午5:39:06<br/>
 * <b>Version</b>: Since JDK 1.6.0<br/>
 * <b>Package</b>: com.ifgostudio.easyweb.framework.web.filter
 */
public class WebContextFilter implements Filter {

	static Log log = LogFactory.getLog(WebContextFilter.class);
	private ServletContext servletContext = null;
	
	/* (non-Javadoc)
	 * @see javax.servlet.Filter#destroy()
	 */
	public void destroy() {
		
	}

	/* (non-Javadoc)
	 * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain filterChain) throws IOException, ServletException {
		WebContext.init();
		Map<String, Object> servletkeeper = WebContext.currentContext().getKeeper();
		servletkeeper.put(WebContextHelper.REQUEST, request);
		servletkeeper.put(WebContextHelper.RESPONSE, response);
		servletkeeper.put(WebContextHelper.SERVLET_CONTEXT, servletContext);
		servletkeeper.put(WebContextHelper.SESSION, ((HttpServletRequest)request).getSession());
		filterChain.doFilter(request, response);
		WebContext.destory();
	}

	/* (non-Javadoc)
	 * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
	 */
	public void init(FilterConfig filterConfig) throws ServletException {
		filterConfig.getServletContext();
		servletContext = filterConfig.getServletContext();
	}

}
